#include"DEMORenderComponentManger.h"

DEMORenderComponentManger* DEMORenderComponentManger::_mngInstance = NULL;

bool DEMORenderComponentManger::isActorInList(DEMORenderComponent* theActor)
{
	for(vector<DEMORenderComponent*>::iterator it = _actorList.begin();it!=_actorList.end();++it)
	{
		if( theActor == *it)
		{
			return true;
		}
	}
	return false;
}

void DEMORenderComponentManger::insertComponentToList(DEMORenderComponent* theComp)
{
	if(isActorInList(theComp))
	{
		return;
	}

	_actorList.push_back(theComp);
}

void DEMORenderComponentManger::removeComponentFromList(DEMORenderComponent* theComp)
{
	for( vector<DEMORenderComponent*>::iterator it = _actorList.begin();it!=_actorList.end();++it)
	{
		if( *it == theComp)
		{
			_actorList.erase( it);
			//it = _actorList.begin();
			return;
		}
	}
}

void DEMORenderComponentManger::tick(float deltaTime)
{
	bool bHeartBeat = isShouldHeartBeat(deltaTime);

	/*for(vector<DEMORenderComponent*>::iterator it = _actorList.begin();it != _actorList.end();++it)
	{
		if( (*it)!= NULL && (*it)->isActive())
		{
			(*it)->tick(deltaTime);
			if( bHeartBeat)
			{
				(*it)->doHeartBeat();
			}
		}
	}*/
}

bool DEMORenderComponentManger::isShouldHeartBeat(float deltaTime)
{
	static float elapsed = 0.0f;

	elapsed += deltaTime;

	if( elapsed >= 0.1f )			//	0.1 is heart beat rate num
	{
		elapsed = 0.0f;
		return true;
	}

	return false;
}

void DEMORenderComponentManger::render()
{
	for(vector<DEMORenderComponent*>::iterator it = _actorList.begin();it != _actorList.end();++it)
	{
		if( (*it)!= NULL && !(*it)->isHidden())
		{
			(*it)->render();
		}
	}
}

void DEMORenderComponentManger::registComponent(DEMORenderComponent* theComp)
{
	if( theComp != NULL )
	{
		insertComponentToList(theComp);
	}
}

void DEMORenderComponentManger::unregistComponent(DEMORenderComponent* theComp)
{
	if( theComp != NULL )
	{
		removeComponentFromList(theComp);
	}
}